/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dream.service.impl;

import com.dream.domain.TaskWork;
import ValidationUtil;
import FileUtil;
import lombok.RequiredArgsConstructor;
import com.dream.repository.TaskWorkRepository;
import com.dream.service.TaskWorkService;
import com.dream.service.dto.TaskWorkDto;
import com.dream.service.dto.TaskWorkQueryCriteria;
import com.dream.service.mapstruct.TaskWorkMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import PageUtil;
import QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author syl
* @date 2020-07-19
**/
@Service
@RequiredArgsConstructor
public class TaskWorkServiceImpl implements TaskWorkService {

    private final TaskWorkRepository taskWorkRepository;
    private final TaskWorkMapper taskWorkMapper;

    @Override
    public Map<String,Object> queryAll(TaskWorkQueryCriteria criteria, Pageable pageable){
        Page<TaskWork> page = taskWorkRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(taskWorkMapper::toDto));
    }

    @Override
    public List<TaskWorkDto> queryAll(TaskWorkQueryCriteria criteria){
        return taskWorkMapper.toDto(taskWorkRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TaskWorkDto findById(Integer id) {
        TaskWork taskWork = taskWorkRepository.findById(id).orElseGet(TaskWork::new);
        ValidationUtil.isNull(taskWork.getId(),"TaskWork","id",id);
        return taskWorkMapper.toDto(taskWork);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TaskWorkDto create(TaskWork resources) {
        return taskWorkMapper.toDto(taskWorkRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TaskWork resources) {
        TaskWork taskWork = taskWorkRepository.findById(resources.getId()).orElseGet(TaskWork::new);
        ValidationUtil.isNull( taskWork.getId(),"TaskWork","id",resources.getId());
        taskWork.copy(resources);
        taskWorkRepository.save(taskWork);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            taskWorkRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TaskWorkDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TaskWorkDto taskWork : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务名称", taskWork.getName());
            map.put("项目名称", taskWork.getItemId());
            map.put("所属公司", taskWork.getCorpId());
            map.put("是否完结", taskWork.getStatus());
            map.put("任务开始日期", taskWork.getStartDate());
            map.put("任务实际开始日期", taskWork.getActualStartTime());
            map.put("任务结束日期", taskWork.getEndDate());
            map.put("任务开始时间具体到小时", taskWork.getStartTime());
            map.put("任务结束时间具体到小时", taskWork.getEndTime());
            map.put("工作时长", taskWork.getWorkTime());
            map.put("工作内容", taskWork.getWorkContent());
            map.put("执行人", taskWork.getTaskUserId());
            map.put("任务状态", taskWork.getTaskStatus());
            map.put("创建时间", taskWork.getCreateTime());
            map.put("创建人", taskWork.getCreateBy());
            map.put("更新时间", taskWork.getUpdateTime());
            map.put("更新人", taskWork.getUpdateBy());
            map.put("备注", taskWork.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}